package cn.com.dhc.danlu.util;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;

import com.xiaomi.mipush.sdk.MiPushClient;

import java.util.List;

import cn.com.dhc.danlu.BuildConfig;
import cn.com.dhc.danlu.dlware.global.DanluApplication;

/**
 * @author wuhaiyang
 */
public class PushEngine {
    /**
     * 如果不用当前指定账号，release的时候需要替换
     *
     * release : 2882303761517471547 、5771747122547
     * develop：2882303761517454563 、 5661745425563
     */
    private static final String DEV_APP_ID = "2882303761517454563";
    private static final String DEV_APP_KEY = "5661745425563";

    private static final String REL_APP_ID = "2882303761517471547";
    private static final String REL_APP_KEY = "5771747122547";
    private static volatile PushEngine mInstance;

    private PushEngine() {
    }

    public static PushEngine newInstance() {
        if (null == mInstance) {
            synchronized (PushEngine.class) {
                if (null == mInstance) {
                    mInstance = new PushEngine();
                }
            }
        }
        return mInstance;
    }

    public void registerPush() {
        if (shouldInit()) {
            if (BuildConfig.push_debug) {
                MiPushClient.registerPush(DanluApplication.get().getApplicationContext(), DEV_APP_ID, DEV_APP_KEY);
            } else {
                MiPushClient.registerPush(DanluApplication.get().getApplicationContext(), REL_APP_ID, REL_APP_KEY);
            }
        }
        //打开Log
//        LoggerInterface newLogger = new LoggerInterface() {
//            @Override
//            public void setTag(String tag) {
//                // ignore
//            }
//            @Override
//            public void log(String content, Throwable t) {
//                Log.w("@@@@ L70", "PushEngine:log() -> " + content);
//            }
//            @Override
//            public void log(String content) {
//                Log.w("@@@@ L70", "PushEngine:log() -> " + content);
//            }
//        };
//        Logger.setLogger(DanluApplication.get().getApplicationContext(), newLogger);
    }

    public boolean shouldInit() {
        ActivityManager am = ((ActivityManager) DanluApplication.get().getSystemService(Context.ACTIVITY_SERVICE));
        List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = DanluApplication.get().getPackageName();
        int myPid = android.os.Process.myPid();
        for (RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    public String getRegisterId() {
        return MiPushClient.getRegId(DanluApplication.get());
    }

    public void setAlias(String userId) {
        // 第三个参数为扩展参数
        MiPushClient.setAlias(DanluApplication.get(), userId, null);
    }

    public void unsetAlias(String alias) {
        MiPushClient.unsetAlias(DanluApplication.get(), alias, null);
    }

    /**
     * 设置标签
     */
    public void setTopic(String topic) {
        MiPushClient.subscribe(DanluApplication.get(), topic, null);
    }

    /**
     * 取消标签设置
     */
    public void unSetTopic(String topic) {
        MiPushClient.unsubscribe(DanluApplication.get(), topic, null);
    }

    public void pausePush() {
        // 扩展参数
        MiPushClient.pausePush(DanluApplication.get(), null);
    }

    public void resumePush() {
        MiPushClient.resumePush(DanluApplication.get(), null);
    }
}